package com.yl.auth.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yl.auth.entity.*;
import com.yl.auth.model.entity.Chuangwei;
import com.yl.auth.model.entity.Laoren;
import com.yl.auth.service.*;
import com.yl.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yl
 * @since 2023-06-15
 */
@Api(tags = "基础设置-床位管理")
@RestController
@RequestMapping("/admin/auth/chuangwei")
public class ChuangweiController {
    @Resource
    private ChuangweiService chuangweiService;
    @Resource
    private LoucengService loucengService;
    @Resource
    private FangjianService fangjianService;
    @Resource
    private LaorenService laorenService;
    @Resource
    private FangjianleixingService fangjianleixingService;
    @Resource
    private YlAdminService ylAdminService;
    @GetMapping("/findAll")
    @ApiOperation(value = "获取全部床位的信息")
    public Result findAll() {
        List<Chuangwei> list = chuangweiService.list();
        return Result.ok(list);
    }
    //1.我们要先查出所有楼层的信息
    //2.再查出每个楼层对应的房间
    //3.在查每个房间对应的床位
    @ApiOperation(value = "查询所有的楼层")
    @GetMapping("/findlouceng/{page}/{limit}")
    public Result findlouceng(@PathVariable @Param("当前页") Integer page, @PathVariable @Param("总条数") Integer limit){
        Page<Louceng> pagefarm = new Page<>(page,limit);
        IPage<Louceng> page1 = loucengService.select(pagefarm);
        return Result.ok(page1);
    }

    @ApiOperation(value = "查询楼层的房间")
    @GetMapping("/findAllfangjian/{id}")
    public Result findAllfangjian(@PathVariable Long id){
        Louceng byId = loucengService.getById(id);
        //根据楼层查出对应的房间
        List<Fangjian> fangjianList = fangjianService.list(new QueryWrapper<Fangjian>().eq("floor_id",byId.getId()));
        return Result.ok(fangjianList);
    }
    @ApiOperation(value = "查询房间的床位")
    @GetMapping("/findAllchaungwei/{id}")
    public Result findAllchaungwei(@PathVariable @ApiParam("房间id") Long id){
        Fangjian byId = fangjianService.getById(id);
        //根据房间查出对应的床位
        List<Chuangwei> chuangweiList = chuangweiService.list(new QueryWrapper<Chuangwei>().eq("room_id",byId.getId()));
        List<Laoren> laorenList=laorenService.list();
        //判断老人id和床位的老人id是否相等，相等的话把老人姓名赋给Mannametwo这个字段
        for (Laoren laoren : laorenList) {
            for (Chuangwei Chuangwei : chuangweiList) {
                if(laoren.getId()==Chuangwei.getLaorenId()){
                    if(laoren.getStatus()==2) {
                        Chuangwei.setMannametwo(laoren.getName());
                    }
                }
            }
        }
        List<Fangjian> fangjianList=fangjianService.list();
        List<Fangjianleixing> fangjianleixingList=fangjianleixingService.list();
        //首先要进行三重循环，将最后得到的房间价格赋给床位中新加的价格字段，然后展示到页面
        for (Chuangwei chuangwei : chuangweiList) {
            for (Fangjian fangjian : fangjianList) {
                for (Fangjianleixing fangjianleixing : fangjianleixingList) {
                    if(chuangwei.getRoomId()==fangjian.getId()){
                        if(fangjian.getRoomType()==fangjianleixing.getId()){
                            chuangwei.setPrice(fangjianleixing.getPrice()*30);
                        }
                    }
                }
            }
        }
        //进行双重循环将房间号和床位进行拼接
        for (Chuangwei chuangwei : chuangweiList) {
            for (Fangjian fangjian : fangjianList) {
                if(chuangwei.getRoomId()==fangjian.getId()){
                    chuangwei.setChuangName(fangjian.getRoomNo()+"-"+chuangwei.getBedNo());
                }
            }
        }
        List<YlAdmin> ylAdminList=ylAdminService.list();
        for (Chuangwei chuangwei : chuangweiList) {
            for (YlAdmin ylAdmin: ylAdminList) {
                if(chuangwei.getAdminId()==ylAdmin.getId()){
                    chuangwei.setName(ylAdmin.getName());
                }
            }
        }
//        for (Laoren laoren : laorenList) {
            for (Chuangwei chuangwei : chuangweiList) {
//                /**
//                 * 0未签约 1预约中 2已入院 3已出院
//                 */
//                if (laoren.getBedId() == chuangwei.getId()) {
                    if (chuangwei.getStatus() == 0) {
                        chuangwei.setStatusName("未签约");
                    }
                    if (chuangwei.getStatus() == 1) {
                        chuangwei.setStatusName("预约中");
                    }
                    if (chuangwei.getStatus() == 2) {
                        chuangwei.setStatusName("已入院");
                    }
                    if (chuangwei.getStatus() == 3) {
                        chuangwei.setStatusName("已出院");
                    }
//                }
//            }
        }
        return Result.ok(chuangweiList);
    }
    //添加床位-----------------------------------------------------
//    @ApiOperation(value = "添加床位信息")
//    @PostMapping("/insertchuang/{id}")
//    public Result insertchuang(@RequestBody Chuangwei chuangwei,@PathVariable Long id){
//        Fangjian byId = fangjianService.getById(id);
//        chuangwei.setRoomId(byId.getId());
//        if(byId.getId()==chuangwei.getRoomId()){
//            chuangwei.setFangname(byId.getRoomNo());
//        }
//        LocalDateTime now = LocalDateTime.now();
//        Integer r=0;
//        long rr=1;
//        chuangwei.setCreateDate(now);
//        chuangwei.setStatus(r);
//        chuangwei.setLaorenId(null);
//        chuangwei.setAdminId(rr);
//        chuangweiService.save(chuangwei);
//        return Result.ok();
//    }
    @ApiOperation(value = "添加床位信息要拿到房间id")
    @PostMapping("/insertchuang")
    public Result insertchuang(@ApiParam("添加房间对象")@RequestBody BedUpdataAndSaveBean bedUpdataAndSaveBean){
        Long id=bedUpdataAndSaveBean.getId();
        String bedNo=bedUpdataAndSaveBean.getBedNo();
        String name=bedUpdataAndSaveBean.getChuangjianname();
        Chuangwei chuangwei=new Chuangwei();
        chuangwei.setBedNo(bedNo);
        chuangwei.setChuangjianname(name);
        Fangjian byId = fangjianService.getById(id);
        chuangwei.setRoomId(byId.getId());
        if(byId.getId()==chuangwei.getRoomId()){
            chuangwei.setFangname(byId.getRoomNo());
        }
        LocalDateTime now = LocalDateTime.now();
        Integer r=0;
        long rr=1;
        chuangwei.setCreateDate(now);
        chuangwei.setStatus(r);
        chuangwei.setLaorenId(null);
        chuangwei.setAdminId(rr);
        chuangweiService.save(chuangwei);
        return Result.ok();
    }
    //删除床位-----------------------------------------------------
    @ApiOperation(value = "删除床位")
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Long id){
        chuangweiService.removeById(id);
        return Result.ok();
    }
    @ApiOperation(value = "根据id查询床位")
    @GetMapping("/getById/{id}")
    public Result getById(@PathVariable Long id){
        Chuangwei chuangwei=chuangweiService.selectById(id);
        return Result.ok(chuangwei);
    }
    //用于修改回显--------------------------------------
    @ApiOperation(value = "根据前台id查询床位信息")
    @GetMapping("/getByIdchaungwei/{id}")
    public Result getByIdchaungwei(@PathVariable Long id){
        Chuangwei byId1 = chuangweiService.getById(id);
        BedUpdataAndSaveBean bedUpdataAndSaveBean=new BedUpdataAndSaveBean();
        bedUpdataAndSaveBean.setBedNo(byId1.getBedNo());
        bedUpdataAndSaveBean.setId(byId1.getRoomId());
        return Result.ok(byId1);
    }
    @ApiOperation(value = "修改床位")
    @PutMapping("/updateById")
    public Result updateById(@RequestBody BedUpdataAndSaveBean bedUpdataAndSaveBean){
        Chuangwei chuangwei=new Chuangwei();
        chuangwei.setBedNo(bedUpdataAndSaveBean.getBedNo());
        chuangwei.setId(bedUpdataAndSaveBean.getId());
        chuangweiService.updateById(chuangwei);
        return Result.ok();
    }
    @GetMapping("/getfindAll")
    @ApiOperation(value = "获取全部状态为未住入床位的信息")
    public Result getfindAll() {
        List<Chuangwei> getfindAll = chuangweiService.getfindAll();
        for (Chuangwei chuangwei : getfindAll) {
            String bedNo = chuangwei.getBedNo();
            LambdaQueryWrapper<Fangjian> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Fangjian::getId,chuangwei.getRoomId());
            List<Fangjian> fangjianList = fangjianService.list(wrapper);
            for (Fangjian fangjian : fangjianList) {
                String roomNo = fangjian.getRoomNo().toString();
                chuangwei.setChuangName(roomNo+"-"+bedNo);
            }
        }
        return Result.ok(getfindAll);
    }

    @GetMapping("/getLaorenChuangWei")
    @ApiOperation(value = "老人床位查询功能")
    public Result getLaorenChuangWei(){
        List<Chuangwei> list = chuangweiService.list();
        for (Chuangwei chuangwei : list) {
            String bedNo = chuangwei.getBedNo();
            LambdaQueryWrapper<Fangjian> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Fangjian::getId,chuangwei.getRoomId());
            List<Fangjian> fangjianList = fangjianService.list(wrapper);
            for (Fangjian fangjian : fangjianList) {
                String roomNo = fangjian.getRoomNo().toString();
                chuangwei.setChuangName(roomNo+"-"+bedNo);
            }
        }
        return Result.ok(list);
    }

}

